package cn.wolfcode.interceptor;


import cn.wolfcode.Utlis.JwtUtlis;
import cn.wolfcode.domain.Result;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取TOken
        String token = request.getHeader("Token");
        try {
            JwtUtlis.parseJwt(token);
            return true;
        } catch (Exception e) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json,charset=utf-8");
            Result result = Result.error("NOT_LOGIN");
            response.getWriter().write(JSON.toJSONString(result));
            return false;
        }

    }

}
